import store from './store'
import {Message} from 'element-plus'
import router from '@/router'
import {getToken} from "@/utils/hongUtils";
//放行数组
const whiteList = ['/login']
//前置拦截
router.beforeEach((to, from, next) => {
    console.log("to",to)
    //白名单放行
    let toPath = to.path;
    if(whiteList.filter(o=>o == toPath).length){
        next();
    }
    let token = getToken();
    console.log("token",token)
    //是否登录
    if(token){
        console.log("store.getters.loginUser",store.getters.loginUser)

        //是否有路由--存在刷新的情况
        if(!(store.getters.loginUser && store.getters.loginUser.username)){
            console.log("store.getters.loginUser",store.getters.loginUser)
            //先获取个人信息
            store.dispatch('getLoginuser').then(res=>{
                //没有路由--生成路由
                store.dispatch('GenerateRoutes').then(res=>{
                    if (res){
                        //放行 放到指定页面
                        next({path:res, replace: true});
                    }else{
                        //放行 放到主页--要重定向，不然不会到新路由
                        next({path:'/', replace: true});
                    }

                }).catch(e=>{
                    //重定向到登录页
                    next({path:'/login', replace: true});
                })
            }).catch(e=>{
                //重定向到登录页
                next({path:'/login', replace: true});
            })
        }else{
            //放行
            next();
        }
    }else{
        //退出登录
        store.dispatch('loginuUserOut').then(res=>{});
        //重定向到登录页
        next({path:'/login'});
    }

})
//后置拦截
router.afterEach(() => {
})
